<title>Bit Manipulation Simulator</title>

<head>
<script language="JavaScript">

function BitWise(form)
{
  // ... Convert the supplied hex numbers to decimal.
  
  var Left  = HexToDec(form.Left.value);
  var Right = HexToDec(form.Right.value);

  // ... Get the operation symbol (& | >> <<);
  
  var Operation  = form.Operation[form.Operation.options.selectedIndex].value;

  Oper = Operation;
  
  // ... Do the calculation in decimal.
  
  var DecAns = eval(Left + Operation + Right);
  
  // ... convert the answer back to Hex format.

  var BinAns = DecToBin(DecAns);        	

  form.Ans.value = BinToHex(BinAns);

  // Butify the inputs.

  BinAns = DecToBin(Left);        	

  form.Left.value = BinToHex(BinAns);

  BinAns = DecToBin(Right);        	

  form.Right.value = BinToHex(BinAns);

  // refresh the screen so the table can display the updated values.

  history.go(0);
}

function HexToDec(Hex)
{
  return(parseInt(Hex, 16));
}

function DecToBin(Decimal)
{
  var Index = 128;		               /* Just look at the 8 
						* low order bits */
  var Binary ="";

  while(Index > 0)
  {
    if (Index == 8) Binary += " ";     /*  Put a space between the  */
						 //  Bytes */

    if((Decimal % Index) != Decimal)
    {
      Binary += "1";
      Decimal -= Index;
    }
    else
    {
      Binary += "0";
    }
    Index /= 2;

    // Bodge...
    
    if (Index == 0.5) Index = 0;
  }
  return(Binary)
}

function BinToHex(Binary)
{
  var Left  = Binary.substring(0,4);
      Left  = BinToHexConv(Left);
  var Right = Binary.substring(5,9);
      Right = BinToHexConv(Right);

return(Left+Right);
}

function BinToHexConv(Binary)
{
  this["0000"] = "0";
  this["0001"] = "1";
  this["0010"] = "2";
  this["0011"] = "3";
  this["0100"] = "4";
  this["0101"] = "5";
  this["0110"] = "6";
  this["0111"] = "7";
  this["1000"] = "8";
  this["1001"] = "9";
  this["1010"] = "A";
  this["1011"] = "B";
  this["1100"] = "C";
  this["1101"] = "D";
  this["1110"] = "E";
  this["1111"] = "F";

  return(this[Binary])
}

</script>
</head>

<body bgcolor="#ffffcc">
<hr>
<center>
<h1>Bit Manipulation Simulator</h1>
</center>
<hr>

This page requires JavaScript support to test the various bitwise operators.
If you do not have JavaScript available, you can try the CGI supported
simulator. Please note. The CGI simulator will only work if the site
administrator has installed it. <blink>Please note, this does not work
when using Internet Explorer 3.0</blink>
<p>

<center>
<font size = +2>
CGI Version
</font>
<table border=2 bgcolor=ivory>
<tr align=center><td>
<a href="/cgi-bin/mjl_bitwork?Left=01&Operator=<<&Right=02">Bit Manipulation Simulator</a>
</td></tr>
</table>
</center>
<p>

<hr><hr>
<p>
<center>
<font size = +2>
JavaScript Version
</font>
<form name="form1">
Enter the Hex values and select an operator, then press the 'Execute'
button to see the results.
<p>

<input type="text"   value="00" name="Left" size=4>
<select name="Operation">
<option value=" & ">  &
<option value=" | ">  |
<option value=" << ">  <<
<option value=" >> ">  >>
</select>

<input type="text"   value="00"      name="Right" size=4>
<input type="Button" value="Execute" name="Ex"    onclick="BitWise(this.form)">
<input type="text"   value="00"      name="Ans"   size=4>
</form>
</center>

<center>
<table border=2 bgcolor="ivory">
<tr align=center>

<td>Hex</td>

<td>Binary</td>

<td>Decimal</td>
</tr>

<tr><td>
<h3>
<script>

// ... When the document is loaded, this is true
// ... and the default values are set.

if (document.form1.Operation.options.selectedIndex == -1)
{
  document.form1.Operation.options.selectedIndex = 0;
  document.form1.Left.value  = 00;
  document.form1.Right.value = 00;
  document.form1.Ans.value   = 00;
}

document.writeln("<pre>");
document.writeln("    "); 
document.writeln("    " + document.form1.Left.value + " ");
document.write  (" " + document.form1.Operation[document.form1.Operation.options.selectedIndex].value);
document.writeln(document.form1.Right.value);
document.writeln("    --");
document.writeln("    " + document.form1.Right.value);
// document.writeln(" " + document.form1.Operation[1].value);
document.writeln("</pre>");
</script>
</h3>
</td>

<td>
<h3>
<script>
document.writeln("<pre>");   
document.writeln("    ");
document.writeln("    " + DecToBin(HexToDec(document.form1.Left.value)) + " ");
document.write  (" " + document.form1.Operation[document.form1.Operation.options.selectedIndex].value);
document.writeln(DecToBin(HexToDec(document.form1.Right.value)));
document.writeln("    ---------");
document.writeln("    " + DecToBin(HexToDec(document.form1.Ans.value)));
document.writeln("</pre>");
</script>
</h3>
</td>

<td>
<h3>
<script>
document.writeln("<pre>");   
document.writeln("    ");
document.writeln("    " + HexToDec(document.form1.Left.value) + "   ");
document.write  (" " + document.form1.Operation[document.form1.Operation.options.selectedIndex].value);
document.writeln(HexToDec(document.form1.Right.value));
document.writeln("    --");
document.writeln("    " + HexToDec(document.form1.Ans.value));
document.writeln("</pre>");
</script>
</h3>
</td></tr>
</table>
</center>
<p>

<font color=red>
Sections of this page controlled by 'JavaScript' cannot be printed.
</font>
<p>
<hr>
<h2>See Also:</h2>
<img src=../../GRAPHICS/whiteball.gif>
<a href=../CONCEPT/bitwise.html>Bitwise Operators</a>.
<P>
<hr>
<p>
<center>
<table border=2 width=80% bgcolor=ivory>
<tr align=center>
<td width=25%>
<a href="../cref.html">Top</a>
</td><td width=25%>
<a href="../master_index.html">Master Index</a>
</td><td width=25%>
<a href="../SYNTAX/keywords.html">Keywords</a>
</td><td width=25%>
<a href="../FUNCTIONS/funcref.htm">Functions</a>
</td>
</tr>
</table>
</center>
<p>
<hr>
<address><a href="../../address.html">Martin Leslie</a> 
<script language="JavaScript">
<!--  //
document.write(document.lastModified);
// -->
</script>
</address>
</body
